
var FormValidation = function () {
	
    return {
        //main function to initiate the module
        init: function () {

            var form2 = $('#form_sample_2');
            var error2 = $('.alert-error', form2);
            var success2 = $('.alert-success', form2);

            form2.validate({
                errorElement: 'span', //default input error message container
                errorClass: 'help-inline', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                ignore: "",
                rules: {
                    rule_type: {
                        required: true,                      
                    },
                    cust_name: {
                        required: true,
                        //mobile:true  //自定义验证标签
                    },
                    order_period:{
                    	required:true,
                    },
                    sales_org:{
                    	required:true,
                    },
                    create_company:{
                    	required:true,
                    },
                    clerk:{
                    	required:true,
                    },
                    cust_type:{
                    	required:true,
                    }, 
                    payment: {
                        required: true
                    },                  
                    currency: {
                        required: true
                    },                  
                    deliver: {
                        required: true
                    },                 
                    address_id: {
                        required: true,
                    },
                    supplier:{
                    	required: true,
                    },
                    invoice_type: {
                        required: true
                    },
                    
                    service: {
                        required: true,
                        minlength: 2
                    }
                },
				//错误验证信息 name值
                messages: { 
                    rule_type: {
                        required: "请选择业务流程",
                    },
                    cust_name:{
                    	required: "请选择客户名称"
                    },
                    order_period:{
                    	required:"请选择一种订单账期"
                    },
                    sales_org:{
                    	required:"请选择一种销售组织"
                    },
                    create_company:{
                    	required:"请选择一种开单账套"
                    },
                    clerk:{
                    	required:"请选择一个业务人员"
                    },
                    cust_type:{
                    	required:"请选择一种客户属性"
                    },
                    payment:{
                    	required:"请选择一种支付方式"
                    },
                    currency:{
                    	required:"请选择一种支付方式"
                    },
                    deliver:{
                    	required:"请选择一种配送方式"
                    },
                    address_id:{
                    	required:"请选择要配送的地址"
                    },
                    supplier:{
                    	required:"请选择一家发货公司"
                    },
                    invoice_type:{
                    	required:"请选择一种发票类型"
                    }
                },

                errorPlacement: function (error, element) { // render error placement for each input type
                    if (element.attr("name") == "billtype") {                     	
                    	// 发票类型
                        error.addClass("no-left-padding").insertAfter("#form_2_membership_error");
                    } else if (element.attr("name") == "service") { // for uniform checkboxes, insert the after the given container
                        error.addClass("no-left-padding").insertAfter("#form_2_service_error");
                    } else {
                        error.insertAfter(element); // for other inputs, just perform default behavoir
                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit   
                    success2.hide();
                    error2.show();
                    App.scrollTo(error2, -200);
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.help-inline').removeClass('ok'); // display OK icon
                    $(element)
                        .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
                },

                unhighlight: function (element) { // revert the change dony by hightlight
                    $(element).closest('.control-group').removeClass('error'); // set error class to the control group
                },

                success: function (label) {
                    if (label.attr("for") == "service" || label.attr("for") == "membership") { // for checkboxes and radip buttons, no need to show OK icon
                        label
                            .closest('.control-group').removeClass('error').addClass('success');
                        label.remove(); // remove error label here
                    } else { // display success icon for other inputs
                        label
                            .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
                        .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
                    }
                },

                
                submitHandler:function(form){
                	console.info($('#form_sample_2').serialize())
                    $.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        }
                    });
                	//提交
					$.ajax({
						url: "/order/createpost",
						type : "POST", 
            			dataType :"json",
						data:$('#form_sample_2').serialize(),
						success: function(data){
                            layer.open({
                              type: 2,
                              title: '订单创建成功',
                              offset: 't',
                              shadeClose: true,
                              shade: 0.8,
                              cancel: function(){ 
                                ///order/list
                              },
                              area: ['966px', '500px'],
                              content: '/order/success/?basic_id='+data.id //iframe的url
                            }); 
			                success2.show();
			                error2.hide();
						},
		               	error: function(data){
							layer.msg('ERROR!',{icon:2,time:1500});
						}
					});
				}

            });
			
			//新增收货地址 验证	
            var error3 = $('.alert-error', $('#form_address'));
            var success3 = $('.alert-success', $('#form_address'));
			$('#form_address').validate({
				errorElement: 'span', //default input error message container
                errorClass: 'help-inline', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                ignore: "",
                rules: {
                    areakind: {
                        required: true,                      
                    },
                    detail: {
                        required: true,
                    },
                    name:{
                    	required:true,
                    },
                    mobile:{
                    	required:true,
                    	mobile:true  //自定义验证标签
                    }
                    
                },
				//错误验证信息 name值
                messages: { 
                    areakind: {
                        required: "请选择您的所在地区",
                    },
                    detail:{
                    	required: "详细地址不能为空"
                    },
                    name:{
                    	required:"收货人姓名不能为空"
                    },
                    mobile:{
                    	required:"手机号码不能为空"
                    }                   
                },

                errorPlacement: function (error, element) { // render error placement for each input type
                    if (element.attr("name") == "qt") {                     	
                    	// 发票类型
                        error.addClass("no-left-padding").insertAfter("#form_2_membership_error");
                    } else if (element.attr("name") == "service") { // for uniform checkboxes, insert the after the given container
                        error.addClass("no-left-padding").insertAfter("#form_2_service_error");
                    } else {
                        error.insertAfter(element); // for other inputs, just perform default behavoir
                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit   
                    success3.hide();
                    error3.show();
                    App.scrollTo(error2, -200);
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.help-inline').removeClass('ok'); // display OK icon
                    $(element)
                        .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
                },

                unhighlight: function (element) { // revert the change dony by hightlight
                    $(element).closest('.control-group').removeClass('error'); // set error class to the control group
                },

                success: function (label) {
                    if (label.attr("for") == "service" || label.attr("for") == "membership") { // for checkboxes and radip buttons, no need to show OK icon
                        label
                            .closest('.control-group').removeClass('error').addClass('success');
                        label.remove(); // remove error label here
                    } else { // display success icon for other inputs
                        label
                            .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
                        .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
                    }
                },
                submitHandler:function(form){
	                var area = $.trim($(".c-areakind").val());
				  	var dess = $.trim($(".c-detailress").val());
				  	var rename = $.trim($(".c-rename").val())
				  	var telphone = $.trim($(".c-telphone").val())
				  	var landtel = $.trim($(".c-landtel").val());
                    var pick_area = $('.pick-area');
                    var area_id = pick_area.attr('data-areacode');
                    var area_name = pick_area.val();
				  	if(!area){
				  		layer.msg("请选择省份");
				  		return false;
				  	}else{
				  		console.info("选择的省份:"+ area)
				  	}
                    $.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        }
                    });
	            	//提交
					$.ajax({
						url: "/order/ajaxCreateAddr",
						type : "POST", 
	        			dataType :"HTML",
						data:$('#form_address').serialize()+'&cust_id='+$('#cust_id').val()+'&area_id='+area_id+'&address='+area_name,
						success: function(data){
                            get_addr_by_cust($('#cust_id').val());
						},
		               	error: function(data){
							layer.msg('ERROR!',{icon:2,time:1000});
						}
					});
				},
				
            });
			
			
            //apply validation on chosen dropdown value change, this only needed for chosen dropdown integration.
            $('.chosen, .chosen-with-diselect', form2).change(function () {
                form2.validate().element($(this)); //revalidate the chosen dropdown value and show error or success message for the input
            });

             //apply validation on select2 dropdown value change, this only needed for chosen dropdown integration.
            $('.select2', form2).change(function () {
                form2.validate().element($(this)); //revalidate the chosen dropdown value and show error or success message for the input
            });

        }

    };

}();